package presentation.windowsFackData;


import java.awt.Font ;
import java.rmi.RemoteException ;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
import javax.swing.JOptionPane ;
import javax.swing.JPanel;
import javax.swing.JPasswordField ;
import javax.swing.JSpinner;
import javax.swing.JTextField ;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingWorker ;
import javax.swing.border.BevelBorder ;

import com.toedter.calendar.JDateChooser;

import presentation.UIUser ;
import presentation.windows.SharedUIData;
import util.DateTools;
import util.Log ;
import util.NotFoundException ;
import util.Subsystem ;
import domain.user.BadPasswordException ;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * The Class LoginPanel is the panel for login to the forum.
 */
public class LoginFakePanel extends BasePanelFake
{
	private static final long serialVersionUID = 2009030180395190295L ;

	/** The Cancel button pressed for clear the components of the page. */
	private JButton CancelButton ;

	/** The Login button pressed for logging in the forum. */
	private JButton LoginButton ;

	/** The login label. */
	private JLabel loginLabel ;
	private JSpinner hourSpinner;
	private JPanel datePanel;

	/** The password label. */
	private JLabel passwordLabel ;

	/** The password text field. */
	private JPasswordField passwordTextField ;

	/** The user name label. */
	private JLabel userNameLabel ;

	/** The user name text field. */
	private JTextField userNameTextField ;

	private JDateChooser dateOfLogin ;
	private JButton fakeButton;

	/**
	 * Creates the login panel
	 * @param sharedData the shared data
	 *
	 */
	public LoginFakePanel(final SharedUIData sharedData)
	{
		super(sharedData) ;
		this.setPreferredSize(new java.awt.Dimension(735, 566)) ;
		this.initGUI() ;
	}

	/**
	 * Initiates all GUI of the login panel
	 */
	public void initGUI()
	{

		this.loginLabel = new JLabel() ;
		this.add(this.loginLabel) ;
		this.loginLabel.setText("Login") ;
		this.loginLabel.setBounds(350, 39, 66, 30) ;
		this.loginLabel.setFont(new Font("Californian FB", 1, 22)) ;

		this.userNameLabel = new JLabel() ;
		this.add(this.userNameLabel) ;
		this.userNameLabel.setText("username") ;
		this.userNameLabel.setFont(new Font("Californian FB", 1, 16)) ;
		this.userNameLabel.setBounds(86, 90, 76, 30) ;

		this.passwordLabel = new JLabel() ;
		this.add(this.passwordLabel) ;
		this.passwordLabel.setText("password") ;
		this.passwordLabel.setFont(new Font("Californian FB", 1, 16)) ;
		this.passwordLabel.setBounds(86, 159, 76, 30) ;

		this.userNameTextField = new JTextField() ;
		this.add(this.userNameTextField) ;
		this.userNameTextField.setBounds(205, 97, 166, 21) ;
		this.userNameTextField.setBorder(BorderFactory.createCompoundBorder(null, BorderFactory
			.createBevelBorder(BevelBorder.LOWERED))) ;

		this.passwordTextField = new JPasswordField() ;
		this.add(this.passwordTextField) ;
		this.passwordTextField.setBounds(205, 166, 166, 21) ;
		this.passwordTextField.setBorder(BorderFactory.createCompoundBorder(null, BorderFactory
			.createBevelBorder(BevelBorder.LOWERED))) ;

		this.LoginButton = new JButton() ;
		this.add(this.LoginButton) ;
		this.LoginButton.setText("Login") ;
		this.LoginButton.setBounds(205, 316, 84, 35) ;

		this.CancelButton = new JButton() ;
		this.add(this.CancelButton) ;
		this.CancelButton.setText("Reset") ;
		this.CancelButton.setBounds(378, 316, 84, 35) ;
		{
			this.datePanel = new JPanel();
			this.add(this.datePanel);
			this.datePanel.setBounds(462, 97, 101, 21);
			
			this.dateOfLogin = new JDateChooser();
			this.datePanel.add(this.dateOfLogin);
			this.dateOfLogin.setPreferredSize(new java.awt.Dimension(107, 17));
		}
		{
			this.hourSpinner = new JSpinner((new SpinnerNumberModel(14, 0, 23, 1)));
			this.add(this.hourSpinner);
			this.hourSpinner.setBounds(462, 145, 101, 21);
		}
		{
			this.fakeButton = new JButton();
			this.add(this.fakeButton);
			this.fakeButton.setText("fake login");
			this.fakeButton.setBounds(462, 189, 101, 24);
		}

		// set button handlers:
		this.LoginButton.addActionListener(new java.awt.event.ActionListener()
		{
			@SuppressWarnings("synthetic-access")
			public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt)
			{
				new SwingWorker<Void,Void>(){
	     			@Override
	     			protected Void doInBackground(){
	     				LoginFakePanel.this.loginButtonHandler() ;
	     				return null;
	     			}
	     			@Override
	    			protected void done() { /* there is nothing to do here .. */ }
	    		}.execute();
			}
		}) ;
		
		
		this.fakeButton.addActionListener(new java.awt.event.ActionListener()
		{
			@SuppressWarnings("synthetic-access")
			public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt)
			{
				new SwingWorker<Void,Void>(){
	     			@Override
	     			protected Void doInBackground(){
	     				LoginFakePanel.this.fakeLoginButtonHandler() ;
	     				return null;
	     			}
	     			@Override
	    			protected void done() { /* there is nothing to do here .. */ }
	    		}.execute();
			}
		}) ;
		

		// set button handlers:
		this.CancelButton.addActionListener(new java.awt.event.ActionListener()
		{
			@SuppressWarnings("synthetic-access")
			public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt)
			{
				LoginFakePanel.this.cancelButtonHandler() ;
			}
		}) ;
	}

	/**
	 * Cancel button handler pressed when the user does not want to login.
	 * The fields are cleaned up
	 */
	private void cancelButtonHandler()
	{
		this.userNameTextField.setText("") ;
		this.passwordTextField.setText("") ;

	}
	/**
	 * Login button handler pressed when the user enter username and password and want to login the forum.
	 * This function also check the data normalcy
	 */
	private void loginButtonHandler()
	{
		final String username = this.userNameTextField.getText() ;
		final String password = new String(this.passwordTextField.getPassword()) ;

		if ((username.equals("")) || (password.equals("")))
		{
			JOptionPane.showMessageDialog(LoginFakePanel.this, "missing username or password!",
				"error occured", JOptionPane.ERROR_MESSAGE) ;
			this.userNameTextField.setText("") ;
			this.passwordTextField.setText("") ;
		}
		else
		{
			try
			{
				final UIUser member = this.forum.login(username, password) ;
				this.forum.unregisterGuest(this.getSharedData().getUser().getId()); //before login the user is always a guest!
				this.getSharedData().setUser(member) ;
				this.getSharedData().setLoggedIn(true) ;
				Log.getLogger(Subsystem.PRESENTATION_MODEL).finest("user " + username + " with id " +member.getId()+ " has logged in");
				this.notifyCompleted();
			}
			catch (final RemoteException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("Remote problems thrown in login panel") ;
				JOptionPane.showMessageDialog(LoginFakePanel.this,
					"Communication problems, please try again", "error occured", JOptionPane.ERROR_MESSAGE) ;
			}
			catch (final NotFoundException e)
			{
				JOptionPane.showMessageDialog(LoginFakePanel.this, "Username is not exist , register first",
					"error occured", JOptionPane.ERROR_MESSAGE) ;
				this.userNameTextField.setText("") ;
				this.passwordTextField.setText("") ;
			}
			catch (final BadPasswordException e)
			{
				JOptionPane.showMessageDialog(LoginFakePanel.this, "Incorrect password!", "error occured",
					JOptionPane.ERROR_MESSAGE) ;
				this.passwordTextField.setText("") ;
				this.userNameTextField.setText("") ;
			}
		}
	}
	
	private void fakeLoginButtonHandler()
	{
		final String username = this.userNameTextField.getText() ;
		Date loginDate = DateTools.set(DateTools.set(DateTools.set(LoginFakePanel.this.dateOfLogin.getDate(), Calendar.SECOND, 0), Calendar.MINUTE, 0), Calendar.HOUR_OF_DAY, (Integer)LoginFakePanel.this.hourSpinner.getValue()) ;
		try
		{
			this.forum.putLogin(username, loginDate);
		}
		catch (RemoteException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("Remote problems thrown in login panel") ;
			JOptionPane.showMessageDialog(LoginFakePanel.this,
				"Communication problems, please try again", "error occured", JOptionPane.ERROR_MESSAGE) ;
		}
	}
}
